package com.ruoyi.evaluate.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.evaluate.domain.vo.EvaluateVO;
import com.ruoyi.order.domain.Order;
import com.ruoyi.order.mapper.OrderMapper;
import com.ruoyi.user.domain.User;
import com.ruoyi.user.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.evaluate.mapper.EvaluateMapper;
import com.ruoyi.evaluate.domain.Evaluate;
import com.ruoyi.evaluate.service.IEvaluateService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 【评价】Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-08
 */
@Service
public class EvaluateServiceImpl implements IEvaluateService 
{
    @Autowired
    private EvaluateMapper evaluateMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public Evaluate selectEvaluateById(Long id)
    {
        return evaluateMapper.selectEvaluateById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param evaluate 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<Evaluate> selectEvaluateList(Evaluate evaluate)
    {
        return evaluateMapper.selectEvaluateList(evaluate);
    }

    @Override
    public EvaluateVO selectEvaluate(Evaluate evaluate)
    {
        EvaluateVO evaluateVO = new EvaluateVO();
        List<Evaluate> evaluates = evaluateMapper.selectEvaluateList(evaluate);
        if (!evaluates.isEmpty()) {
            Evaluate evaluate1 = evaluates.get(0);
            String image = evaluate1.getImage();
            BeanUtils.copyProperties(evaluate1, evaluateVO);
            if (image != null) {
                List<String> imageList = Arrays.asList(image.split(","));
                evaluateVO.setImageList(imageList);
            }
        }
        return evaluateVO;
    }

    @Override
    public List<EvaluateVO> selectEvaluates(Evaluate evaluate)
    {
        ArrayList<EvaluateVO> evaluateVOS = new ArrayList<>();
        List<Evaluate> evaluates = evaluateMapper.selectEvaluateList(evaluate);
        for (Evaluate evaluate1 : evaluates) {
            EvaluateVO evaluateVO = new EvaluateVO();
            BeanUtils.copyProperties(evaluate1, evaluateVO);
            User user = userMapper.selectUserByUserId(evaluate1.getFromUserId());
            evaluateVO.setUser(user);
            evaluateVOS.add(evaluateVO);
        }
        return evaluateVOS;
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param evaluate 【请填写功能名称】
     * @return 结果
     */
    @Override
    @Transactional
    public int insertEvaluate(Evaluate evaluate)
    {
        Order order = orderMapper.selectOrderByOrderId(evaluate.getOrderId());
        evaluate.setToUserId(order.getExpertId());
        order.setIsEvaluate("0");
        orderMapper.updateOrder(order);
        orderMapper.updateOrder(order);
        evaluate.setCreateTime(DateUtils.getNowDate());
        return evaluateMapper.insertEvaluate(evaluate);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param evaluate 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateEvaluate(Evaluate evaluate)
    {
        return evaluateMapper.updateEvaluate(evaluate);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteEvaluateByIds(Long[] ids)
    {
        return evaluateMapper.deleteEvaluateByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteEvaluateById(Long id)
    {
        return evaluateMapper.deleteEvaluateById(id);
    }
}
